<?php
require_once("./bm_header.php");
$is_admin = isset($_SESSION['administrator']) || isset($_SESSION[$BM_ADMIN]);
if (isset($_GET['cid'])) 
{
    $cid        = intval($_GET['cid']);
    $contest_id = $cid;
    $view_cid   = $cid;
    if (isset($_GET['school']))
        $sele_school = intval($_GET['school']);
    if (isset($_GET['xmkind']))
        $sele_xmkind = intval($_GET['xmkind']);
    require_once("./judgecid.php");
    
    /***读取所有学校名称***/
    $sql             = "SELECT `team_school` FROM `cr_team` WHERE `team_cid` = $cid GROUP BY `team_school` ORDER BY `team_school`";
    $result          = mysql_query($sql);
    $sele_school_cnt = 0;
    while ($row = mysql_fetch_array($result))
    {
        $to_sele_school[$sele_school_cnt] = $row['team_school'];
        $sele_school_cnt++;
    }
    mysql_free_result($result);
    
    
    /***读取所有项目类别***/
    $sql             = "SELECT * FROM `cr_xmkind` WHERE `contest_id` = $cid ORDER BY `xmkind_num` ASC";
    $result          = mysql_query($sql);
    $sele_xmkind_cnt = 0;
    while ($row = mysql_fetch_array($result)) 
    {
        $to_sele_xmkind[$sele_xmkind_cnt] = $row['xmkind_name'];
        $sele_xmkind_cnt++;
    }
    mysql_free_result($result);
    
    
    
    /***如果有ckind，即进入了具体比赛类型的查询***/
    if (isset($_GET['ckind'])) 
    {
        $ckind = $_GET['ckind'];
        /***查看算法赛***/
        if ($ckind == 0) 
		{
			require_once("template/".$BM_TEMPLATE."/results_suanfa.php");
		
		
		/*
            if (isset($_GET['school']) && $sele_school >= 0 && $sele_school < $sele_school_cnt) {
                $cschool = $to_sele_school[$sele_school];
                $sql     = "
				SELECT * FROM `cr_team`
				WHERE `team_cid`='$contest_id' AND `team_school` = '$cschool' AND `team_kind` = 0
				ORDER BY `team_id` ASC
				";
            } 
            else 
            {
                $sql = "
				SELECT * FROM `cr_team`
				WHERE `team_cid`='$contest_id' AND `team_kind` = 0
				ORDER BY `team_school` ASC
				";
            }
            
            $cnt = 0;
            while ($row = mysql_fetch_array($result)) {
                $cnt++;
                $team_id     = $row['team_id'];
                $team_school = SqlToHtml($row['team_school']);
                $team_name   = SqlToHtml($row['team_name']);
                $team_coach  = SqlToHtml($row['team_coach']);
                $team_phone  = SqlToHtml($row['team_phone']);
                if ($is_admin) {
                    echo "<tr><td rowspan = '3'><nobr>$cnt</nobr></td><td rowspan = '3'><nobr>$team_school</nobr></td><td rowspan = '3'><nobr>$team_name</nobr></td><td rowspan = '3'><nobr>$team_coach</nobr></td><td rowspan = '3'><nobr>$team_phone</nobr></td>";
                    $sql  = "
				SELECT * FROM `cr_stu` 
				WHERE `team_id`='$team_id'
				ORDER BY `stu_123` ASC
				";
                    $lcnt = 0;
                    $lres = mysql_query($sql);
                    while ($lrow = mysql_fetch_array($lres)) {
                        $lcnt++;
                        if ($lcnt > 3)
                            break;
                        $team_id    = SqlToHtml($row['team_id']);
                        $stu_card   = SqlToHtml($lrow['stu_card']);
                        $stu_number = SqlToHtml($lrow['stu_number']);
                        $stu_name   = SqlToHtml($lrow['stu_name']);
                        $stu_major  = SqlToHtml($lrow['stu_major']);
                        $stu_grade  = SqlToHtml($lrow['stu_grade']);
                        $stu_sex    = SqlToHtml($lrow['stu_sex']);
                        $stu_cloth  = ($lrow['stu_cloth']);
                        echo "<td><nobr>$stu_card</nobr></td><td><nobr>$stu_number</nobr></td><td><nobr>$stu_name</nobr></td><td><nobr>$stu_major</nobr></td><td><nobr>$stu_grade</nobr></td><td><nobr>" . ($stu_sex ? "女" : "男") . "</nobr></td><td><nobr>$stu_cloth</nobr></td>";
                        if ($lcnt == 1)
                            echo "<td rowspan = '3'><nobr><a href= '" . $BM_HOME . "/admin_delete.php?cid=$cid&tid=$team_id' onclick='return confirm(\"删除是不可恢复的，确认要删除吗？\");'>删除</a></nobr></td></tr>";
                        else
                            echo "</tr>";
                    }
                    while ($lcnt < 3) {
                        $lcnt++;
                        echo "<td><nobr>未填写</nobr></td><td><nobr>未填写</nobr></td><td><nobr>未填写</nobr></td><td><nobr>未填写</nobr></td><td><nobr>未填写</nobr></td><td><nobr>" . ($stu_sex ? "女" : "男") . "</nobr></td><td><nobr>未填写</nobr></td>";
                        if ($lcnt == 1)
                            echo "<td rowspan = '3'><nobr><a href= '" . $BM_HOME . "/admin_delete.php?cid=$cid&tid=$team_id'>删除</a></nobr></td></tr>";
                        else
                            echo "</tr>";
                    }
                    
                    mysql_free_result($lres);
                } else {
                    echo "<tr><td ><nobr>$team_id</nobr></td><td ><nobr>$team_school</nobr></td><td ><nobr>$team_coach</nobr></td><td><nobr>$team_phone</nobr></td></tr>";
                }
            }
            
            mysql_free_result($result);
			*/
        }
        /***ckind不是0，列出项目赛***/
        else if ($ckind == 1) 
        {
            if (isset($_GET['tid'])) 
            {
                if (!$is_admin) 
                {
                    echo "<script language='javascript'>\n";
                    echo "alert('没有查看权限!');\n";
                    echo "history.go(-1);\n";
                    echo "</script>";
                    mysql_free_result($result);
                    exit(0);
                }
                $team_id   = intval($_GET['tid']);
                $sql       = "SELECT * FROM `cr_team` WHERE `team_id` = $team_id";
                $result    = mysql_query($sql);
                $row       = mysql_fetch_object($result);
                $team_name = SqlToHtml($row->team_name);
                mysql_free_result($result);
                
                $sql    = "SELECT * FROM `cr_stu` WHERE `team_id`=$team_id";
                $result = mysql_query($sql);
				require_once("template/".$BM_TEMPLATE."/results_xiangmu_stu.php");
 				
                mysql_free_result($result);
            } 
            else 
            {
                $sql    = "SELECT * FROM `cr_xmkind` WHERE `contest_id`= $cid ORDER BY `xmkind_num` ASC";
                $result = mysql_query($sql);
                $i      = 0;
                while ($row = mysql_fetch_object($result)) 
                {
                    $i++;
                    $xmkind_name[$i] = $row->xmkind_name;
                }
                mysql_free_result($result);
                $sql = "SELECT * FROM `cr_team` WHERE  `team_kind`!=0 AND `team_cid`=$cid";
                
                if (isset($_GET['school']) && $sele_school >= 0 && $sele_school < $sele_school_cnt) 
                {
                    $cschool = $to_sele_school[$sele_school];
                    $sql .= " AND `team_school`= '$cschool'";
                }
                if (isset($_GET['xmkind']) && $sele_xmkind >= 0 && $sele_xmkind < $sele_xmkind_cnt) 
                {
                    $cxmkind = $sele_xmkind + 1;
                    $sql .= " AND `team_kind`= $cxmkind";
                }
                $sql .= " ORDER BY `team_school` ASC";
                $result = mysql_query($sql);
                
                $i   = 0;
				require_once("template/".$BM_TEMPLATE."/results_xiangmu.php");
                /*
                while ($row = mysql_fetch_object($result)) 
                {
                    $i++;
                    $team_xmkind = $xmkind_name[$row->team_kind];
                    if ($is_admin) {
                        echo "
							<tr><td><nobr>$i</nobr></td>
							<td><nobr>$row->team_id</nobr></td>
							<td><nobr>$row->team_school</nobr></td>
							<td><nobr>$row->team_name</nobr></td>
							<td><nobr>$row->team_coach</nobr></td>
							<td><nobr>$row->team_phone</nobr></td>
							<td><nobr>$team_xmkind</nobr></td>
							<td><nobr>$row->team_xmname</nobr></td>
							<td><nobr><a href='" . $BM_HOME . "/results.php?cid=$cid&ckind=1&tid=$row->team_id'>查看队员</a></td>
							";
                        if (strlen($row->team_upload) == 0)
                            echo "<td><nobr><font color=red>未上传</font></nobr></td>";
                        else
                            echo "<td><nobr><a href = '" . $row->team_upload . "'>下载项目</td>";
                        echo "<td><nobr><a href='" . $BM_HOME . "/coach_delete.php?cid=$cid&tid=$row->team_id'>删除</a></td></tr>";
                    } else {
                        echo "
						<tr><td><nobr>$i</nobr></td>
								<td><nobr>$row->team_id</nobr></td>
								<td><nobr>$row->team_school</nobr></td>
								<td><nobr>$row->team_name</nobr></td>
								<td><nobr>$row->team_coach</nobr></td>
								<td><nobr>$row->team_phone</nobr></td>
								<td><nobr>$team_xmkind</nobr></td>		
						";
                    }
                }
                */
            }
        }
    }
    /***如果没有ckind，则列出两种比赛的入口***/
    else 
    { 
		require_once("template/".$BM_TEMPLATE."/results_enter.php");
    }
} 
else 
{
    echo "<script language='javascript'>\n";
    echo "alert('这儿什么都没有~~');\n";
    echo "history.go(-1);\n";
    echo "</script>";
    exit(0);
}

require_once("bm_footer.php");
?>
